package org.dizem.groupon.dao.impl;

import org.dizem.groupon.dao.OrderFormDAO;
import org.dizem.groupon.model.domain.OrderFormEntity;
import org.dizem.groupon.model.domain.UserEntity;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import java.util.Date;
import java.util.List;

/**
 * User: dizem
 * Time: 11-7-21 下午2:37
 */
public class OrderFormDAOImpl extends DAOImplSupport<OrderFormEntity> implements OrderFormDAO {
	@Override
	public OrderFormEntity getById(int id) {
		return (OrderFormEntity) getHibernateTemplate().get(OrderFormEntity.class, id);
	}

	@Override
	public List<OrderFormEntity> loadAll() {
		return getHibernateTemplate().loadAll(OrderFormEntity.class);
	}

	@Override
	public List<OrderFormEntity> load(int firstResult, int maxResult) {
		return getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(OrderFormEntity.class), firstResult, maxResult);
	}

	public List<OrderFormEntity> getByUser(UserEntity user) {
		DetachedCriteria dc = DetachedCriteria.forClass(OrderFormEntity.class);
		dc.add(Restrictions.eq("user", user));
		return getHibernateTemplate().findByCriteria(dc);
	}

	public List<OrderFormEntity> getByItemId(int itemId) {
		DetachedCriteria dc = DetachedCriteria.forClass(OrderFormEntity.class);
		dc.add(Restrictions.eq("itemId", itemId));
		return getHibernateTemplate().findByCriteria(dc);
	}

	public int getWaitingCount() {
		DetachedCriteria dc = DetachedCriteria.forClass(OrderFormEntity.class);
		dc.add(Restrictions.eq("status", 0));
		return getHibernateTemplate().findByCriteria(dc).size();
	}

	public int getSuccessCount() {
		DetachedCriteria dc = DetachedCriteria.forClass(OrderFormEntity.class);
		dc.add(Restrictions.eq("status", 1));
		return getHibernateTemplate().findByCriteria(dc).size();
	}

	public int getTodayCount() {
		Date today = new Date();
		today.setHours(0);
		today.setMinutes(0);
		today.setSeconds(0);
		DetachedCriteria dc = DetachedCriteria.forClass(OrderFormEntity.class);
		dc.add(Restrictions.ge("orderTime", today));
		return getHibernateTemplate().findByCriteria(dc).size();
	}

	public List<OrderFormEntity> getWaitingList(int firstResult, int maxResult) {
		DetachedCriteria dc = DetachedCriteria.forClass(OrderFormEntity.class);
		dc.add(Restrictions.eq("status", 0));
		return getHibernateTemplate().findByCriteria(dc, firstResult, maxResult);
	}

	public List<OrderFormEntity> getSuccessList(int firstResult, int maxResult) {
		DetachedCriteria dc = DetachedCriteria.forClass(OrderFormEntity.class);
		dc.add(Restrictions.eq("status", 1));
		return getHibernateTemplate().findByCriteria(dc, firstResult, maxResult);
	}

	public List<OrderFormEntity> getOrderFormsToday(int firstResult, int maxResult) {
		Date today = new Date();
		today.setHours(0);
		today.setMinutes(0);
		today.setSeconds(0);
		DetachedCriteria dc = DetachedCriteria.forClass(OrderFormEntity.class);
		dc.add(Restrictions.ge("orderTime", today));
		return getHibernateTemplate().findByCriteria(dc, firstResult, maxResult);
	}

	public void acceptOrder(int id) {
		OrderFormEntity order = getById(id);
		if(order != null) {
			order.setStatus(1);
			update(order);
		}
	}
}
